/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service;

import java.math.BigDecimal;
import java.util.*;

import com.jeesite.modules.st.entity.StWarehousePositionSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.StWarehouseInventory;
import com.jeesite.modules.st.dao.StWarehouseInventoryDao;

/**
 * 实时库存Service
 *
 * @author HOHD
 * @version 2020-12-16
 */
@Service
@Transactional(readOnly = true)
public class StWarehouseInventoryService extends CrudService<StWarehouseInventoryDao, StWarehouseInventory> {

    /**
     * 获取单条数据
     *
     * @param stWarehouseInventory
     * @return
     */
    @Override
    public StWarehouseInventory get(StWarehouseInventory stWarehouseInventory) {
        return super.get(stWarehouseInventory);
    }

    /**
     * 查询分页数据
     *
     * @param stWarehouseInventory 查询条件
     * @return
     */
    @Override
    public Page<StWarehouseInventory> findPage(StWarehouseInventory stWarehouseInventory) {
        return super.findPage(stWarehouseInventory);
    }

    @Autowired
    @Lazy
    StWarehouseInventoryService stWarehouseInventoryService;

    @Autowired
    StMaterialRecordService stMaterialRecordService;

    @Autowired
    StProductRecordService stProductRecordService;

    @Autowired
    StWarehousePositionSettingService stWarehousePositionSettingService;

    public List<StWarehouseInventory> getGoodsNumber(StWarehouseInventory stWarehouseInventoryTemp) {
//        StMaterialRecord entity = new StMaterialRecord();
//        entity.setMaterialCode(code);
//        List<StMaterialRecord> list1 = stMaterialRecordService.findList(entity);
//
//        StProductRecord productRecord = new StProductRecord();
//        productRecord.setProductCode(code);
//        List<StProductRecord> list2 = stProductRecordService.findList(productRecord);
//        String id = null;
//        if (Constant.warehouse_goods_type.material == goodsType) {
//            id = list1.get(0).getId();
//        } else if (Constant.warehouse_goods_type.complete_product == goodsType) {
//            id = list2.get(0).getId();
//        }

//        StWarehouseInventory stWarehouseInventoryTemp = new StWarehouseInventory();
//        stWarehouseInventoryTemp.setBatchNumber(batchNumber);
//        stWarehouseInventoryTemp.setMaterialCode(code);

        List<StWarehouseInventory> list = stWarehouseInventoryService.getWarehouseNumber(true, stWarehouseInventoryTemp);
        List<StWarehouseInventory> wList = new ArrayList<>();
        List<StWarehousePositionSetting> list1 = stWarehousePositionSettingService.findList(new StWarehousePositionSetting());
        Map<String, String> warehouseNameMap = new HashMap<>();
        Map<String, String> warehouseIdMap = new HashMap<>();
        for (StWarehousePositionSetting stWarehousePositionSetting : list1) {
            warehouseNameMap.put(stWarehousePositionSetting.getWarehouseNameEN(), stWarehousePositionSetting.getWarehouseName());
        }
        for (StWarehousePositionSetting stWarehousePositionSetting : list1) {
            warehouseIdMap.put(stWarehousePositionSetting.getWarehouseNameEN(), stWarehousePositionSetting.getId());
        }
        //只有一个批次的一个物品
        //盘点也要用
        if (!list.isEmpty()) {
            for (StWarehouseInventory stWarehouseInventory1 : list) {

                Map<String, Integer> warehouseNumber = stWarehouseInventory1.getWarehouseNumber();
                Set<Map.Entry<String, Integer>> entries = warehouseNumber.entrySet();
                String materialName = stWarehouseInventory1.getMaterialName();
                String materialCode = stWarehouseInventory1.getMaterialCode();
                String batchNumber = stWarehouseInventory1.getBatchNumber();
                //添加唯一id
                for (Map.Entry<String, Integer> entry : entries) {
                    StWarehouseInventory stWarehouseInventory = new StWarehouseInventory();
                    String key = entry.getKey();
                    stWarehouseInventory.setId(key);//只有一条记录的时候才能这样干，很明显错了
                    stWarehouseInventory.setWarehouseId(warehouseIdMap.get(key));
                    String name = warehouseNameMap.get(key);
                    stWarehouseInventory.setWarehouseName(name);
                    Integer value = entry.getValue();
                    stWarehouseInventory.setNumber(new BigDecimal(value + ""));
                    stWarehouseInventory.setMaterialName(materialName);
                    stWarehouseInventory.setMaterialCode(materialCode);
                    stWarehouseInventory.setBatchNumber(batchNumber);
                    Integer canZero = stWarehouseInventoryTemp.getCanZero();
                    if (null == canZero) {
                        if (0 != value)
                            wList.add(stWarehouseInventory);
                    } else wList.add(stWarehouseInventory);
                }
            }
        }
        for (int i = 0; i < wList.size(); i++) {
            wList.get(i).setId(i + "");
        }
        return wList;
    }


//    public List<StWarehouseInventory> getWarehouseNumberAll(StWarehouseInventory stWarehouseInventoryTemp) {
//        List<StWarehouseInventory> list = getWarehouseNumber(true,stWarehouseInventoryTemp);
//        for (int i = 0; i < list.size(); i++) {
//            list.get(i).setId(i + "");
//        }
//        return list;
//    }

    public List<StWarehouseInventory> getWarehouseNumber(boolean isDetail, StWarehouseInventory stWarehouseInventoryTemp) {
        List<StWarehouseInventory> warehouseNumber = dao.getWarehouseNumber(stWarehouseInventoryTemp);
        Map<String, StWarehouseInventory> mapCode = new HashMap<>();
//        Map<String, StWarehouseInventory> mapCodeBatch = new HashMap<>();
        Map<String, StWarehouseInventory> mapUsed = mapCode;
        for (StWarehouseInventory stWarehouseInventory : warehouseNumber) {
            String materialCode = stWarehouseInventory.getMaterialCode();
            String batchNumber = stWarehouseInventory.getBatchNumber();
            if (isDetail) {
                materialCode += "_" + batchNumber;
            } else {
                //抹批号
                stWarehouseInventory.setBatchNumber("");
            }
            Set<String> keys = mapUsed.keySet();
            if (!keys.contains(materialCode)) {
                Map<String, Integer> warehouseN = new HashMap<>();
                stWarehouseInventory.setWarehouseNumber(warehouseN);
                mapUsed.put(materialCode, stWarehouseInventory);
            }
            StWarehouseInventory stWarehouseInventoryUse = mapUsed.get(materialCode);//名字映射好，
            Map<String, Integer> warehouseN = stWarehouseInventoryUse.getWarehouseNumber();// 合并到其中一项
            int number = stWarehouseInventory.getNumber().intValue();
            String warehouse = stWarehouseInventory.getWarehouse();//英文名
            Integer prime = warehouseN.get(warehouse);
            if (null == prime) {
                prime = 0;
            }
            warehouseN.put(warehouse, number + prime);
        }
        Collection<StWarehouseInventory> valueCollection = mapUsed.values();

        List<StWarehouseInventory> list = new ArrayList<>(valueCollection);
        return list;
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param stWarehouseInventory
     */
    @Override
    @Transactional(readOnly = false)
    public void save(StWarehouseInventory stWarehouseInventory) {
        super.save(stWarehouseInventory);
    }

    /**
     * 更新状态
     *
     * @param stWarehouseInventory
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(StWarehouseInventory stWarehouseInventory) {
        super.updateStatus(stWarehouseInventory);
    }

    /**
     * 删除数据
     *
     * @param stWarehouseInventory
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(StWarehouseInventory stWarehouseInventory) {
        super.delete(stWarehouseInventory);
    }

}